<?php
//***************  add by code   ******************
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of article
 *
 * @author Administrator
 */
class product extends CI_Controller {

    private $page_size=10;
    private $parentid;

    public function __construct() {
        parent::__construct();
        $this->url = $this->uri->segments;
        $this->CI = & get_instance();
    }

    public function index() {
        $this->CI->load->library('a_product');
        $g_nav_list = $this->CI->load->get_var('g_nav_list');//获取导航
        $url_info = $this->url;
        $action = $url_info[2];
        $data['action_info'] = $this->m_public->getRowInfo('class',array('classlinkname'=>$action));
        $data['brand_list'] = $this->m_public->getAllInfo('class',array('parentid'=>$data['action_info']['classid']));
        foreach($data['brand_list'] as $key=>$val){
            $cate_list = $this->m_public->getAllInfo('class',array('parentid'=>$val['classid']));
            foreach($cate_list as $k=>$v){
                $product_condition = array(
                    'from' => 'product',
                    'where' => array('classid'=>$v['classid'],'isrecommend'=>1)
                );
                $cate_list[$k]['product_lists'] = $this->m_public->get_list($product_condition);
//                $cate_list[$k]['product_lists'] = $this->m_public->getAllInfo('product',array('classid'=>$v['classid']));
            }
            $data['brand_list'][$key]['cate_list'] = $cate_list;
        }
        $condition['from'] = 'link';
        $condition['where'] = array('typeid'=>2,'configid'=>CONFIG_ID);
        $condition['order'] = 'listid';
        $data['links'] = $this->m_public->get_list($condition);

        /*SEO部分*/
        $config = $this->m_public->getRowInfo('config',array('configid'=>CONFIG_ID));
        $data['g_site']['websitename'] = $config['websitename'];
        $data['g_site']['websitekeywords'] = $config['websitekeywords'];
        $data['g_site']['websitedescription'] = $config['websitedescription'];
        if(isset($data['action_info']['seotitle']) && !empty($data['action_info']['seotitle']))
            $data['g_site']['websitename'] = $data['action_info']['seotitle'];
        if(isset($data['action_info']['keywords']) && !empty($data['action_info']['keywords']))
            $data['g_site']['websitekeywords'] = $data['action_info']['keywords'];
        if(isset($data['action_info']['description']) && !empty($data['action_info']['description']))
            $data['g_site']['websitedescription'] = $data['action_info']['description'];

        /*右侧部分，联系我们*/
        $contact_config = $this->m_public->getRowInfo('config',array('configid'=>CONFIG_ID),'contact_config');
        $data['contact'] = json_decode($contact_config['contact_config'],true);

        /*头部,底部数据*/
        $config = $this->m_public->getRowInfo('config',array('configid'=>CONFIG_ID),'copyright,banquan,contact_config,footerinfo');
        $config['contact_config'] = json_decode($config['contact_config'],true);
        $data['config'] = $config;
        $footer_condition = array(
            'from' => 'link',
            'where' => array('isclose'=>0,'typeid'=>1,'configid'=>CONFIG_ID),
            'order' => 'listid'
        );
        $data['footer_info'] = $this->m_public->get_list($footer_condition);

        $this->load->view('template_product_cate',$data);
    }

    public function detail($id) {
        if($id){
            $this->load->library('a_product');
            $product_obj = new a_product();
            $data['product_info'] = $product_obj->get_product($id);

            $data['parent_info'] = $this->m_public->getRowInfo('class',array('classid'=>$data['product_info']['classid']));//产品分类
            $data['parent_parent_info'] = $this->m_public->getRowInfo('class',array('classid'=>$data['parent_info']['parentid']));//所属产品品牌
            $data['parent_parent_brother_info'] = $this->m_public->getAllInfo('class',array('parentid'=>$data['parent_parent_info']['parentid']));//产品品牌list
            $data['parent_brother_info'] = $this->m_public->getAllInfo('class',array('parentid'=>$data['parent_info']['parentid']));//分类list
            $data['parent_parent_parent_info'] = $this->m_public->getRowInfo('class',array('classid'=>$data['parent_parent_info']['parentid']));

            /*SEO部分*/
            $config = $this->m_public->getRowInfo('config',array('configid'=>CONFIG_ID));
            $data['g_site']['websitename'] = $config['websitename'];
            $data['g_site']['websitekeywords'] = $config['websitekeywords'];
            $data['g_site']['websitedescription'] = $config['websitedescription'];
            /*var_dump($data['product_info']);exit;*/
            if(isset($data['product_info']['title']) && !empty($data['product_info']['title']))
                $data['g_site']['websitename'] = $data['product_info']['title'];
            if(isset($data['product_info']['keywords']) && !empty($data['product_info']['keywords']))
                $data['g_site']['websitekeywords'] = $data['product_info']['keywords'];
            if(isset($data['product_info']['description']) && !empty($data['product_info']['description']))
                $data['g_site']['websitedescription'] = $data['product_info']['description'];

            /*右侧部分，联系我们*/
            $contact_config = $this->m_public->getRowInfo('config',array('configid'=>CONFIG_ID),'contact_config');
            $data['contact'] = json_decode($contact_config['contact_config'],true);

            /*头部,底部数据*/
            $config = $this->m_public->getRowInfo('config',array('configid'=>CONFIG_ID),'copyright,banquan,contact_config,footerinfo');
            $config['contact_config'] = json_decode($config['contact_config'],true);
            $data['config'] = $config;

            /*导航选中状态*/
            $this->get_parent_id($data['product_info']['classid']);
            $data['parentid'] = $this->parentid;

            $this->load->view('product_detail',$data);
        }else{
            redirect('cn/main/nofont');
        }
    }

    public function get_parent_id($id){
        if($id){
            $info = $this->m_public->getRowInfo('class',array('classid'=>$id));
            $parent_info = $this->m_public->getRowInfo('class',array('classid'=>$info['parentid']));
            if(!empty($parent_info)){
                if($parent_info['parentid'] == 0){
                    $this->parentid = $parent_info['classid'];
                }else{
                    $this->get_parent_id($parent_info['classid']);
                }
            }
        }
    }


}
